package com.ilove.friend.frame;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.frame.Frame;

import com.ilove.friend.model.FriendContants;
import com.ilove.friend.model.FriendDataProvider;
import com.ilove.friend.model.FriendList;
import com.ilove.model.SMSender;

public class TopNewFrame implements Frame{
	public static TopNewFrame instance = new TopNewFrame();
	public String sex;// 0 woman, 1 man, 2 all
	public String provincecode;
	public String provincename;
	public String type="0"; // o new / 1 hot
	String message;
	String destination;
	public String[] friendlist = null;
	private final FriendDataProvider dp = new FriendDataProvider();
	private Screen screen;
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}
	public void onAdded() {
		try {
			friendlist = dp.getHotNewFriendList(provincecode, sex,type);
			dp.setFriendList(friendlist);
			// Load the screen
			screen = Kuix.loadScreen(FriendContants.FRIENDLISTFRAME_SCREEN, dp);
			screen.setCurrent();
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	public boolean onMessage(Object identifier, Object[] arg1) {
		if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		}
		else if ("save".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					FriendList friend = (FriendList) dataprovider;
					try {
						message = "HSC7X85 DOWNLOAD_PG " + friend.code;
						destination = "7585";
						Kuix.alert("Bạn tải ảnh phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
										KuixConstants.ALERT_QUESTION
												| KuixConstants.ALERT_YES
												| KuixConstants.ALERT_NO,
										"sendsms", "!exit");
					} catch (Exception e) {
					}
				}
			}
		}
		 else if ("friend".equals(identifier)) {
				Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
				if (focusedWidget instanceof ListItem) {
					DataProvider dataprovider = ((ListItem) focusedWidget)
							.getDataProvider();
					if (dataprovider != null) {
						FriendList friend = (FriendList) dataprovider;
						try {
							message = "HSC7X85 PG " + friend.code;
							destination = "7585";
							Kuix.alert("Bạn kết bạn phai tra phí (5000 đồng). Bạn có đồng ý ko?)",
											KuixConstants.ALERT_QUESTION
													| KuixConstants.ALERT_YES
													| KuixConstants.ALERT_NO,
											"sendsms", "!exit");
						} catch (Exception e) {
						}
					}
				}
			}
		else if ("sendsms".equals(identifier)) {
			new SMSender(message, destination).send();
			return false;
		}
		return false;
	}
	public void onRemoved() {
		try {
			ProvinceFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}
}
